package com.example.designpatterns.strategy.payment;

/**
 * 支付宝支付策略
 */
public class AlipayStrategy implements PaymentStrategy {
    private String email;
    private String password;
    
    /**
     * 创建支付宝支付策略
     * @param email 支付宝账号邮箱
     * @param password 密码
     */
    public AlipayStrategy(String email, String password) {
        this.email = email;
        this.password = password;
    }
    
    @Override
    public boolean pay(double amount) {
        System.out.println("使用支付宝支付: ¥" + amount);
        System.out.println("账号: " + maskEmail());
        // 实际应用中这里会调用支付宝API
        return true;
    }
    
    @Override
    public String getName() {
        return "支付宝支付";
    }
    
    /**
     * 对邮箱账号进行掩码处理
     * @return 掩码后的邮箱
     */
    private String maskEmail() {
        if (email == null || email.isEmpty() || !email.contains("@")) {
            return "***@***.com";
        }
        
        int atIndex = email.indexOf('@');
        String name = email.substring(0, atIndex);
        String domain = email.substring(atIndex);
        
        if (name.length() <= 2) {
            return name + "***" + domain;
        } else {
            return name.substring(0, 2) + "***" + domain;
        }
    }
} 